# -*- coding:utf-8 -*-
# 解析器
from lxml import etree


class HtmlParser(object):
    def parse(self, html_content):
        """
        接收网页的HTML响应，解析，获取想要的数据
        :param html_content: {str}'<html></html>'
        :return: new_urls{list} https://baike.baidu.com/某以词条[/123464/...]
        :return:new_data{data} {'title':title,'summary':summary}
        """
        assert html_content is not None, "html_content 为空 请检查"

        dom = etree.HTML(html_content)   # 解析之后得到HTML的dom文档树

        pattern_title = '//dd[@class="lemmaWgt-lemmaTitle-title"]/h1/text()'
        pattern_summary = '//div[class="lemma-summary"]/div/text()'
        pattern_href = '//div[@class="main-content"]//a[@target="_blank"]/@href'

        title = dom.xpath(pattern_title)
        summary = dom.xpath(pattern_summary)
        new_urls = dom.xpath(pattern_href)    # 返回的url是词条，需要加入域名（https://baike.baidu.com/）补全词条

        for index, href in enumerate(new_urls):
            # todo 判断连接是否和法，可以用正则，或是str.find('item')
            new_urls[index] = 'https://baike.baidu.com' + href

        # 第一种写法（django写法）
        # content = {}
        # content['title'] = title
        # content['summary'] = summary
        # content['new_urls'] = new_urls
        # return content

        # 第二种写法
        new_data = {'title': title, 'summary': summary}
        return new_urls, new_data


















